package data.dao.daoImplementation;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import data.dao.*;
import data.dto.*;
import data.searchdto.*;

public class LeaveDetailDAOImpl implements LeaveDetailDAO {

	@Override
	public LeaveDetailDTO findLeaveDetail(
			LeaveDetailSearchDTO leavedetailsearchdto)
			throws LeaveDAOException, java.sql.SQLException, NotFoundException,
			SQLException {

		String sql = "SELECT * FROM leavedetail WHERE isactive = 1 AND id = ? ";
		PreparedStatement stmt = null;
		LeaveDetailDTO value = null;
		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			stmt.setInt(1, leavedetailsearchdto.getLeaveID());

			value = singleQuery(stmt);

		} finally {
			if (stmt != null)
				stmt.close();
		}
		return value;
	}

	@Override
	public ArrayList<LeaveDetailDTO> findAllLeaveDetail()
			throws LeaveDAOException {
		String sql = "SELECT * FROM leavedetail WHERE isactive = 1 AND id = ? ";
		ArrayList<LeaveDetailDTO> value = new ArrayList<LeaveDetailDTO>();
		ResultSet result = null;
		PreparedStatement stmt = null;
		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			result = util.Database.getInstance().executeQuery(stmt);
			while (result.next()) {
				LeaveDetailDTO valueObject = new LeaveDetailDTO();
				valueObject.setId(result.getInt("id"));
				valueObject.setLeaveID(result.getInt("leaveid"));
				valueObject.setLeaveDetailType(result
						.getShort("leavedetailtype"));
				valueObject.setCreateBy(result.getInt("createdby"));
				valueObject.setCreatedOn(result.getTimestamp("createdon"));
				value.add(valueObject);
			}

		} catch (java.sql.SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
		return value;
	}

	@Override
	public void insertLeaveDetail(LeaveDetailDTO LeaveDetail)
			throws LeaveDAOException, java.sql.SQLException, SQLException {

		String sql = "";
		PreparedStatement stmt = null;

		try {
			sql = "INSERT INTO leavedetail ( leaveid, leavedetailtype, "
					+ "isactive) VALUES (?, ?, ,1) ";
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);

			//stmt.setInt(1, LeaveDetail.getId());
			stmt.setInt(1, LeaveDetail.getLeaveID());
			stmt.setInt(2, LeaveDetail.getLeaveDetailType());
			//stmt.setInt(3, util.CurrentUser.getInstance().getUserId());
			//stmt.setTimestamp(4, util.CurrentDate.getCurrentTimeStamp());

			int rowcount = util.Database.getInstance().executeUpdate(stmt);
			if (rowcount != 1) {
				// System.out.println("PrimaryKey Error when updating DB!");
				throw new SQLException("PrimaryKey Error when updating DB!");
			}

		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	@Override
	public void updateLeaveDetail(LeaveDetailDTO LeaveDetail)
			throws LeaveDAOException, java.sql.SQLException, NotFoundException,
			SQLException {

		String sql = "UPDATE leavedetail SET leaveid = ?, leavedetailtype = ?, "
				+ " WHERE (id = ? ) ";
		PreparedStatement stmt = null;

		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			stmt.setInt(1, LeaveDetail.getLeaveID());
			stmt.setInt(2, LeaveDetail.getLeaveDetailType());

			stmt.setInt(3, LeaveDetail.getId());

			int rowcount = util.Database.getInstance().executeUpdate(stmt);
			if (rowcount == 0) {
				// System.out.println("Object could not be saved! (PrimaryKey not found)");
				throw new NotFoundException(
						"Object could not be saved! (PrimaryKey not found)");
			}
			if (rowcount > 1) {
				// System.out.println("PrimaryKey Error when updating DB! (Many objects were affected!)");
				throw new SQLException(
						"PrimaryKey Error when updating DB! (Many objects were affected!)");
			}
		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	@Override
	public void deleteLeaveDetail(LeaveDetailDTO LeaveDetail)
			throws LeaveDAOException, java.sql.SQLException, NotFoundException,
			SQLException {

		String sql = "UPDATE leavedetail SET isactive = 1 WHERE (id = ? ) ";
		PreparedStatement stmt = null;

		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			stmt.setInt(1, LeaveDetail.getId());

			int rowcount = util.Database.getInstance().executeUpdate(stmt);
			if (rowcount == 0) {
				// System.out.println("Object could not be deleted (PrimaryKey not found)");
				throw new NotFoundException(
						"Object could not be deleted! (PrimaryKey not found)");
			}
			if (rowcount > 1) {
				// System.out.println("PrimaryKey Error when updating DB! (Many objects were deleted!)");
				throw new SQLException(
						"PrimaryKey Error when updating DB! (Many objects were deleted!)");
			}
		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	protected LeaveDetailDTO singleQuery(PreparedStatement stmt)
			throws NotFoundException, SQLException, java.sql.SQLException {

		ResultSet result = null;
		LeaveDetailDTO valueObject = null;

		try {
			result = util.Database.getInstance().executeQuery(stmt);

			if (result.next()) {
				valueObject = new LeaveDetailDTO();
				valueObject.setId(result.getInt("id"));
				valueObject.setLeaveID(result.getInt("leaveid"));
				valueObject.setLeaveDetailType(result
						.getShort("leavedetailtype"));
				//valueObject.setCreateBy(result.getInt("createdby"));
				//valueObject.setCreatedOn(result.getTimestamp("createdon"));

			} else {
				// System.out.println("Leavedetail Object Not Found!");
				throw new NotFoundException("Leavedetail Object Not Found!");
			}
		} finally {
			if (result != null)
				result.close();
			if (stmt != null)
				stmt.close();
		}
		return valueObject;
	}

}
